<template>
  <div>
    <div class="header">
      <el-input
        class="input"
        v-model="searchObj.roleName"
        placeholder="用户名"
      ></el-input>
      <el-button
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-search"
        @click="getRoles()"
        >查询</el-button
      >
      <el-button icon="el-icon-search" @click="clear">清空</el-button>
    </div>
    <div class="margin">
      <el-button type="primary" @click="addRole">添加</el-button>
      <el-button type="danger" @click="delAll" :disabled="!ids.length"
        >批量删除</el-button
      >
      <el-table
        :data="roles"
        v-loading="loading"
        border
        stripe
        class="margin table"
        ref="multipleTable"
        tooltip-effect="dark"
        style="width: 800px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" width="120px">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="info"
              size="mini"
              icon="el-icon-info"
              title="分配角色"
              @click="
                $router.push(
                  `/nested/role/auth/${row.id}?roleName=${row.roleName}`
                )
              "
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editRole(row)"
              title="修改角色"
            ></el-button>
            <el-popconfirm
              title="这是一段内容确定删除吗？"
              @onConfirm="delRole(row)"
            >
              <el-button
                style="margin-left: 10px"
                type="danger"
                icon="el-icon-delete"
                size="mini"
                slot="reference"
                title="删除角色"
              ></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: center"
        class="margin"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[3, 5, 7, 9]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加或者修改用户 -->
    <el-dialog
      @close="dialogClosed"
      :title="!role.id ? '添加用户' : '修改用户'"
      :visible.sync="addRoleDialogVisible"
      width="50%"
    >
      <span slot="footer" class="dialog-footer">
        <el-form :model="role" :rules="roleRule" ref="roleRef">
          <el-form-item label="角色名称" prop="roleName" label-width="100px">
            <el-input v-model="role.roleName"></el-input>
          </el-form-item>
        </el-form>
        <el-button @click="addRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      limit: 3,
      searchObj: {
        roleName: "",
      },
      roles: [],
      loading: false,
      total: 0,
      role: {},
      addRoleDialogVisible: false,
      roleRule: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      ids: [],
    };
  },
  created() {
    this.getRoles();
  },
  methods: {
    async getRoles() {
      this.loading = true;
      const { page, limit, searchObj } = this;
      const res = await this.$Api.role.getPageList(page, limit, searchObj);
      if (res.code == 20000) this.loading = false;
      this.roles = res.data.items;
      this.total = res.data.total;
    },
    async handleSelectionChange(selection) {
      if (!selection.length) {
        this.ids = [];
      } else {
        this.ids = selection.map((item) => JSON.parse(item.id));
      }
    },
    addRole() {
      this.role = {};
      this.addRoleDialogVisible = true;
    },
    // 添加或者修改后点击确定
    addOrUpdate() {
      this.$refs.roleRef.validate(async (valid) => {
        if (valid) {
          const res = await this.$Api.role[
            !this.role.id ? "save" : "updateById"
          ](this.role);
          if (res.code == 20000) {
            this.$message.success("保存成功");
            this.addRoleDialogVisible = false;
            this.getRoles();
          }
        }
      });
    },
    async editRole(row) {
      const res = await this.$Api.role.getById(row.id);
      this.addRoleDialogVisible = true;
      this.role = res.data.item;
    },
    async delRole(row) {
      const res = await this.$Api.role.removeById(row.id);
      if (res.code == 20000) {
        this.getRoles();
        this.$message.success("删除角色成功");
      }
    },
    clear() {
      this.searchObj = {};
      this.getRoles();
    },
    handleSizeChange(newPageNum) {
      this.limit = newPageNum;
      this.getRoles();
    },
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.getRoles();
    },
    // 监听弹出层关闭事件
    dialogClosed() {
      this.$nextTick(() => {
        this.$refs.roleRef.resetFields();
      });
    },
    // 批量删除
    async delAll() {
      const Result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => this.$message.info("取消了删除"));
      if (Result == "confirm") {
        const res = await this.$Api.role.removeRoles(this.ids);
        console.log(res);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;

  .input {
    width: 250px;
  }
}
::v-deep .el-dialog__body {
  padding: 10px 0;
}
</style>
